package com.tedu.ourprojct.controller;

import com.tedu.ourprojct.mapper.FoodMapper;
import com.tedu.ourprojct.pojo.dto.FoodDto;
import com.tedu.ourprojct.pojo.dto.FoodUpdateDto;
import com.tedu.ourprojct.pojo.entity.Food;
import com.tedu.ourprojct.pojo.vo.FoodAdminVO;
import com.tedu.ourprojct.pojo.vo.FoodUpdateVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.Date;
import java.util.List;

/**
 * 餐品信息处理类
 * @author 袁田
 */
@RestController
@RequestMapping("/food")
public class FoodController {
    @Autowired(required = false)
    FoodMapper mapper;


    @RequestMapping("/insert")
    public void insert(@RequestBody FoodDto food){
        System.out.println(food);
        Food f=new Food();
        BeanUtils.copyProperties(food,f);
        f.setCreated(new Date());
        System.out.println(f);
        mapper.insert(f);
    }

    @RequestMapping("/select")
    public List<FoodAdminVO> select(){
        return mapper.selectAdmin();
    }


    @RequestMapping("/update")
    public void update(@RequestBody FoodUpdateDto food) {
        mapper.update(food);
    }


    @RequestMapping("/selectUpdate")
    public FoodUpdateVO selectUpdate(int id){
        return mapper.selectUpdate(id);
    }

    /**
     * 定义本项目的静态文件夹
     */
    @Value("${dirPath}")
    private String dirPath;

    @RequestMapping("/delete")
    public void delete(int id){
        //通过id先查询到图片的名称url
        String url = mapper.selectUrlById(id);
        //删除文件
        new File(dirPath+"/"+url).delete();
        //删除数据库里面的数据
        mapper.deleteById(id);
    }

}
